package com.cby.library.widget.alpha;

import android.content.Context;
import android.util.TypedValue;
import android.view.View;

import androidx.annotation.NonNull;

import com.cby.library.widget.R;

import java.lang.ref.WeakReference;

/**
 * 在 pressed 和 disabled 时改变 View 的透明度
 *
 * @author 徐智伟
 * @create 2018/7/27
 */
public class AlphaViewHelper {

  private WeakReference<View> mTarget;
  /**
   * 设置是否要在 press 时改变透明度
   */
  private boolean mChangeAlphaWhenPress = true;

  /**
   * 设置是否要在 disabled 时改变透明度
   */
  private boolean mChangeAlphaWhenDisable = true;

  private float mNormalAlpha = 1f;
  private float mPressedAlpha = .5f;
  private float mDisabledAlpha = .5f;

  public AlphaViewHelper(@NonNull View target) {
    this.mTarget = new WeakReference<>(target);
    mPressedAlpha = getAttrFloatValue(target.getContext(), R.attr.alpha_pressed);
    mDisabledAlpha = getAttrFloatValue(target.getContext(), R.attr.alpha_disabled);
  }

  public AlphaViewHelper(@NonNull View target, float pressedAlpha, float disabledAlpha) {
    this.mTarget = new WeakReference<>(target);
    mPressedAlpha = pressedAlpha;
    mDisabledAlpha = disabledAlpha;
  }

  /**
   * @param current the view to be handled, maybe not equal to target view
   */
  public void onPressedChanged(View current, boolean pressed) {
    View target = mTarget.get();
    if (target == null) {
      return;
    }
    if (current.isEnabled()) {
      target.setAlpha(
          mChangeAlphaWhenPress && pressed && current.isClickable() ? mPressedAlpha : mNormalAlpha);
    } else {
      if (mChangeAlphaWhenDisable) {
        target.setAlpha(mDisabledAlpha);
      }
    }
  }

  /**
   * @param current the view to be handled, maybe not  equal to target view
   */
  public void onEnabledChanged(View current, boolean enabled) {
    View target = mTarget.get();
    if (target == null) {
      return;
    }
    float alphaForIsEnable;
    if (mChangeAlphaWhenDisable) {
      alphaForIsEnable = enabled ? mNormalAlpha : mDisabledAlpha;
    } else {
      alphaForIsEnable = mNormalAlpha;
    }
    if (current != target && target.isEnabled() != enabled) {
      target.setEnabled(enabled);
    }
    target.setAlpha(alphaForIsEnable);
  }

  /**
   * 设置是否要在 press 时改变透明度
   *
   * @param changeAlphaWhenPress 是否要在 press 时改变透明度
   */
  public void setChangeAlphaWhenPress(boolean changeAlphaWhenPress) {
    mChangeAlphaWhenPress = changeAlphaWhenPress;
  }

  /**
   * 设置是否要在 disabled 时改变透明度
   *
   * @param changeAlphaWhenDisable 是否要在 disabled 时改变透明度
   */
  public void setChangeAlphaWhenDisable(boolean changeAlphaWhenDisable) {
    mChangeAlphaWhenDisable = changeAlphaWhenDisable;
    View target = mTarget.get();
    if (target != null) {
      onEnabledChanged(target, target.isEnabled());
    }

  }

  public float getAttrFloatValue(Context context, int attrRes){
    TypedValue typedValue = new TypedValue();
    context.getTheme().resolveAttribute(attrRes, typedValue, true);
    return typedValue.getFloat();
  }
}
